package com.vma.jifu.backsys.controller;

import com.jfinal.plugin.activerecord.Page;
import com.vma.jifu.bizModel.model.Offer;
import com.vma.jifu.bizModel.model.OfferType;
import com.vma.tools.ReturnsUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by kitabi on 2017/6/20.
 */
public class OfferTypeController extends BaseController{

    public void add(){
        try {
            String brandName = getPara("brandName");
            int indexs= getParaToInt("indexs");
            new OfferType().setCreateTime(new Date()).setUpdateTime(new Date()).setTypeName(brandName).setIndexs(indexs).save();
            renderJson(ReturnsUtil.returnSuccess("新增类别成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }

    }

    public void update(){
        try {
            String offerTypeName = getPara("offerTypeName");
            int indexs= getParaToInt("indexs");
            OfferType.dao.findById(getPara("id")).setTypeName(offerTypeName).setIndexs(indexs).update();
            renderJson(ReturnsUtil.returnSuccess("修改类别成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }

    }
    public void delete(){
        try {
            List offers = Offer.dao.find("select * from offer where offer_type = ? and  status in (0,1) ",getPara("id"));
            if(offers == null || offers.size() == 0){
                OfferType.dao.findById(getPara("id")).delete();
                renderJson(ReturnsUtil.returnSuccess("删除"));
            }else {
                renderJson(ReturnsUtil.returnFail("该品牌存在商品,不可删除，请先删除商品","-1"));

            }

        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }

    }

    public void gryTypeList(){
        try {
            String typeName = getPara("typeName");

            int pageNo = getParaToInt("cPage");
            int pageSize = getParaToInt("pSize");

            List<String> param = new ArrayList<String>();
            String condition = "";


            if(typeName != null && !"".equals(typeName)){
                condition += "and type_name like ? ";
                param.add("%"+typeName+"%");
            }
            Page<OfferType> offerTypePage = OfferType.dao.paginate(pageNo,pageSize,"select * ","from offer_type where 1=1  "+condition+
                            " order by indexs asc"
                    ,param.toArray());

            renderJson(ReturnForm(offerTypePage.getList(),offerTypePage.getTotalRow()));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void gryTypeListAll(){
        try {

            List<OfferType> offerTypePage = OfferType.dao.find("select * from offer_type ");

            renderJson(offerTypePage);
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }
}
